package com.wanmait.translate.system.service.impl;

import com.wanmait.translate.dto.ResultData;
import com.wanmait.translate.pojo.SystemDept;
import com.wanmait.translate.pojo.SystemPost;
import com.wanmait.translate.system.mapper.SystemDeptMapper;
import com.wanmait.translate.system.mapper.SystemPostMapper;
import com.wanmait.translate.system.service.SystemPostService;
import com.wanmait.translate.utils.CommonMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class SystemPostServiceImpl implements SystemPostService {
    @Resource
    private SystemPostMapper systemPostMapper;
    @Resource
    private SystemDeptMapper systemDeptMapper;

    private Logger LOG= LoggerFactory.getLogger(SystemPostServiceImpl.class);
    /*
     * @Author huweibin
     * @Description 获得岗位及父岗位、部门信息
     * @Date 10:03 2020/11/11
     **/
    @Override
    public SystemPost getSelfAndParentByPrimaryKey(Integer id) {
        SystemPost post = systemPostMapper.getByPrimaryKey(id);
        SystemPost parentPost =null;
        if (post.getParentId()==0){
            parentPost = new SystemPost();
            parentPost.setId(0);
            parentPost.setName(CommonMessage.ROOT);
        }else {
            parentPost = systemPostMapper.getBaseByPrimaryKey(post.getParentId());
        }
        post.setParent(parentPost);
        return post;
    }

    /*
     * @Author huweibin
     * @Description 获得岗位信息
     * @Date 10:16 2020/11/11
     **/
    @Override
    public SystemPost getByPrimaryKey(Integer id) {
        if (id==0){
            SystemPost systemPost = new SystemPost();
            systemPost.setName(CommonMessage.ROOT);
            systemPost.setId(0);
            return systemPost;
        }
        return systemPostMapper.getByPrimaryKey(id);
    }


    /**
     * @Author huweibin
     * @Description  查找此部门下所有岗位
     * @Date 18:27 2020/11/12
     **/
    @Override
    public List<SystemPost> listByConditionAndDeptId(SystemPost post) {
        Integer deptId=post.getDeptId().getId();
        //如果是查询所有，就直接查所有返回
        if (deptId ==0){
           return  systemPostMapper.listByCondition(post);
        }
        List<SystemDept> res = new ArrayList<>();
        //获得以deptId为顶级id的所有子部门
        List<SystemDept> depts=systemDeptMapper.listTopByDeptId(deptId);
        //如果不是根部门，连此部门一起查
        if (deptId!=0){
            depts.add(systemDeptMapper.getByPrimaryKey(deptId));
        }
        SystemDept mid=null;
        //递归展开
        for (SystemDept systemDept : depts) {
            loop(systemDept,res);
        }

        return systemPostMapper.listByDeptId(deptId);
    }
    /*
     * @Author huweibin
     * @Description 保存
     * @Date 20:37 2020/11/12
     **/
    @Override
    public void save(SystemPost systemPost) {
        systemPostMapper.save(systemPost);
    }

    @Override
    public ResultData<SystemPost> remove(Integer id) {
        List<Integer> list= systemPostMapper.getChildPost(id);
        if (list.size()==0){
            systemPostMapper.remove(id);
            return ResultData.successsWithOutData();
        }
        return ResultData.fail("删除父岗位前，必须清空子岗位");

    }

    @Override
    public void update(SystemPost systemPost) {
        //更新之前的对象
        SystemPost before = systemPostMapper.getByPrimaryKey(systemPost.getId());
        //获得原先所有的子节点,更新所有子节点的父节点
        List<Integer> childMenu = systemPostMapper.getChildPost(systemPost.getId());
        SystemPost post = new SystemPost();
        for (Integer id : childMenu) {
            //设置原先的父节点为子节点的父节点
            post.setParentId(before.getParentId());
            post.setId(id);
            LOG.info("更新对象为"+post);
            systemPostMapper.update(post);
        }
        systemPostMapper.update(systemPost);
    }

    /**
     * @Author huweibin
     * @Description 递归函数 systemDept当前节点 res结果集
     * @Date 16:54 2020/11/12
     **/
    private  void loop(SystemDept systemDept,List<SystemDept> res){
        if (systemDept!=null){
            List<SystemDept> depts = systemDept.getDepts();
            if (depts!=null&&depts.size()!=0){
                for (SystemDept dept : depts) {
                    loop(dept,res);
                }
            }
            res.add(systemDept);
        }
    }
}
